package a16_ArrayCaculateValue;

import java.util.Random;

/**
 * @Author quan
 * @Description 遍历数组求和
 * @Date 2023/2/1 17:34
 */
public class SumAverageCount {
    public static void main(String[] args) {
        /*需求：生成10个1-100之间的随机数存入数组
        (1)求所有数据的和
        (2)求所有数据的平均数
        (3)统计有多少个数据比平均值小*/

        //动态初始化数组
        int[] arr = new int[10];
        //生成随机数据
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            //每循环一次，生成一个新的随机数
            int num = r.nextInt(100)+1;
            //将生成的随机数赋值给数组
            arr[i] = num;
        }
        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }

        //求和
        int sum = sumArray(arr);

        //求平均数
        double average = averageArray(sum,arr);

        //统计个数
        int count = countFun(arr,average);
        System.out.println(sum+","+average+","+count);

    }

    public static int countFun(int arr[],double average){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]<average){
                count++;
            }
        }
        return count;
    }

    public static double averageArray(int sum,int[] arr){
        return (double) sum/(arr.length);
    }
    public static int sumArray(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum +=arr[i];
        }
        return sum;
    }
}
